#include "tools.h"
#include "book_list.h"

BookList  BookIndex(0,"index");//全局目录*******************************************

BookList::BookList(void) {}
BookList::BookList(unsigned int id,const char* name):m_id(id),m_name(name){}

//输入一个已存在书号,检索目录，将这本书添加到书单
bool BookList::addBook(unsigned int book_id)
{
	map<unsigned int , BookInfo>::iterator it = BookIndex.mapBookInfo.find(book_id);
	if(it == BookIndex.mapBookInfo.end())
	{
		return false;
	}
	mapBookInfo.insert(make_pair(book_id,it->second));
	return true;
}

//添加一本新的书
bool BookList::addBook(Book& book)
{
	string name;
	name.assign(book.m_name);
	string author;
	author.assign(book.m_author);
	unsigned int id;
	id = book.m_id;
	string type;
	type.assign(book.m_type);
	
	map<unsigned int , BookInfo>::iterator it = mapBookInfo.find(id);
	if(it == mapBookInfo.end())
	{
		BookInfo bookinfo(name.c_str(),author.c_str(),id,type.c_str());
		mapBookInfo.insert(make_pair(id,bookinfo));
		return true;
	}
	return false;
}

//输入一个书号，从书单中删除
bool BookList::delBook(unsigned int book_id)
{
	map<unsigned int , BookInfo>::iterator it = mapBookInfo.find(book_id);
	if(it == mapBookInfo.end())
	{
		return false;
	}
	mapBookInfo.erase(book_id);
	return true;
}

//输入一个书名查找书号
vector<BookInfo> BookList::findBook(const char* book_name)
{
	vector<BookInfo> all_book;
	map<unsigned int , BookInfo>::iterator it;
	for( it = mapBookInfo.begin(); it != mapBookInfo.end(); it++ )
	{
		BookInfo bookinfo = it->second;
		if(string::npos != bookinfo.m_name.find(book_name,0))
		{
			all_book.push_back(bookinfo);
		}
	}
	return all_book;
}

unsigned int BookList::getId(void)
{
	return m_id;
}

void BookListSave(BookList& booklist)
{
	char path[128] = {};
	sprintf(path,"../data/booklist/%d.txt",booklist.m_id);
	FILE* fwp = fopen(path,"w");
	fprintf(fwp,"ID:%d ",booklist.m_id);
	fprintf(fwp,"书单名:%s\n",booklist.m_name.c_str());
	fprintf(fwp,"数量:%d\n",booklist.mapBookInfo.size());
	for(map<unsigned int,BookInfo>::iterator it = booklist.mapBookInfo.begin();it != booklist.mapBookInfo.end();it++)
	{
		fprintf(fwp,"书号:%d ",it->second.m_id);
		fprintf(fwp,"书名:%s ",it->second.m_name.c_str());
		fprintf(fwp,"作者:%s ",it->second.m_author.c_str());
		fprintf(fwp,"类别:%s\n",it->second.m_type.c_str());
	}
	fclose(fwp);
}
BookList* BookListLoad(unsigned int list_id)
{
	char path[128] = {};
	sprintf(path,"../data/booklist/%d.txt",list_id);
	FILE* frp = fopen(path,"r");
	if(frp == NULL ) return NULL;
	BookList* booklist = new BookList;
	fscanf(frp,"ID:%d ",&booklist->m_id);
	char buf[256] = {};
	fscanf(frp,"书单名:%s\n",buf);
	booklist->m_name = buf;
	int size = 0;
	fscanf(frp,"数量:%d\n",&size);
	for(int i=0;i<size ;i++)
	{
		BookInfo info;
		fscanf(frp,"书号:%d ",&info.m_id);
		bzero(buf,256);
		fscanf(frp,"书名:%s ",buf);
		info.m_name = buf;
		bzero(buf,256);
		fscanf(frp,"作者:%s ",buf);
		info.m_author = buf;
		bzero(buf,256);
		fscanf(frp,"类别:%s\n",buf);
		info.m_type = buf;
		unsigned int id = info.m_id;
		booklist->mapBookInfo.insert(make_pair(id,info));
	}
	fclose(frp);
	return booklist;
}


void IndexSave(void)
{
	FILE* fwp = fopen("../data/index.txt","w");
	fprintf(fwp,"ID:%d ",BookIndex.m_id);
	fprintf(fwp,"书单名:%s\n",BookIndex.m_name.c_str());
	fprintf(fwp,"数量:%d\n",BookIndex.mapBookInfo.size());
	for(map<unsigned int,BookInfo>::iterator it = BookIndex.mapBookInfo.begin();it != BookIndex.mapBookInfo.end();it++)
	{
		fprintf(fwp,"书号:%d ",it->second.m_id);
		fprintf(fwp,"书名:%s ",it->second.m_name.c_str());
		fprintf(fwp,"作者:%s ",it->second.m_author.c_str());
		fprintf(fwp,"类别:%s\n",it->second.m_type.c_str());
	}
	fclose(fwp);
}
void IndexLoad(void)
{
	FILE* frp = fopen("../data/index.txt","r");
	if(frp == NULL ) return ;
	fscanf(frp,"ID:%d ",&BookIndex.m_id);
	char buf[256] = {};
	fscanf(frp,"书单名:%s\n",buf);
	BookIndex.m_name = buf;
	int size = 0;
	fscanf(frp,"数量:%d\n",&size);
	for(int i=0;i<size ;i++)
	{
		BookInfo info;
		fscanf(frp,"书号:%d ",&info.m_id);
		bzero(buf,256);
		fscanf(frp,"书名:%s ",buf);
		info.m_name = buf;
		bzero(buf,256);
		fscanf(frp,"作者:%s ",buf);
		info.m_author = buf;
		bzero(buf,256);
		fscanf(frp,"类别:%s\n",buf);
		info.m_type = buf;
		BookIndex.mapBookInfo.insert(make_pair(info.m_id,info));
	}
	fclose(frp);
}









